package com.jierui.meter.frontend.web.rest.front;

import com.jierui.meter.frontend.domain.VO.*;
import com.jierui.meter.frontend.service.CalInfoService;
import com.jierui.meter.frontend.service.TErrorComputingService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jierui.meter.commons.Constants.*;

/**
 * @description: 计算结果
 * @author: wangjingjing
 * @creat: 2021-06-17 10:52
 */
@RestController
@RequestMapping("/frontend")
public class CalInfoResource {

    @Autowired
    private CalInfoService calInfoService;

    @Autowired
    TErrorComputingService tErrorComputingService;

    private static final Log log = LogFactory.getLog(CalInfoResource.class);

    /**
     * 1.2 获取总览界面数据
     * 调试参数（header 参数为 date 2021-6-18 其他参数为空 以下接口一致）
     * http://localhost:9001/frontend/dashboard
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/dashboard")
    public Map<String, Object> getDashboard(HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();

        //获取时间
        String year = request.getHeader("dateStr");
        resMap.put(STATUS, ERROR);

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }

        Map<String, Object> data = calInfoService.getDashboard(year, scope, orgNo7);


        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     * 2.1 附加
     * 调试参数（header 参数为 date 2021-6-18 其他参数为空 以下接口一致）
     * http://localhost:9001/frontend/sa/result
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/sa/result")
    public Map<String, Object> getResult(HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();

        //获取时间
        String year = request.getHeader("dateStr");
        resMap.put(STATUS, ERROR);

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }

        List<ResultTableVO> data = calInfoService.getResultNoPage(year, scope, orgNo7);


        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     * 调试参数（header 参数为 date 2021-6-18 其他参数为空 以下接口一致）
     * http://localhost:9001/frontend/sa/result/1/5
     * @param request
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/sa/result/{page}/{size}")
    public Map<String, Object> getResult(HttpServletRequest request, @PathVariable Integer page, @PathVariable Integer size) {
        Map<String, Object> resMap = new HashMap<>();

        //获取时间
        String year = request.getHeader("dateStr");
        resMap.put(STATUS, ERROR);

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (null == page || null == size) {
            resMap.put(DES,"page and size parameter is empty!");
            return resMap;
        }

        Map<String, Object> data = calInfoService.getResult(year, scope, orgNo7, page, size);


        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     * 2.2超超差电能表按节点名模糊查询/按电能表/资产编号模糊查询(已调)
     * 调试url 可参考 http://localhost:9001/frontend/sa/errmeter/selectbyorg/_/03/1/5/测试
     * @param selelcttype
     * @param calerror
     * @param checkstatus
     * @param input
     * @param page
     * @param size
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/sa/errmeter/{selelcttype}/{calerror}/{checkstatus}/{page}/{size}/{input}", method=RequestMethod.GET)
    public Map<String,Object> errmeter(@PathVariable String selelcttype,@PathVariable String calerror,
                                       @PathVariable String checkstatus, @PathVariable String input,
                                       @PathVariable Integer page,@PathVariable Integer size,
                                       HttpServletRequest request){

        Map<String,Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);

        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (null == page || null == size) {
            resMap.put(DES,"page and size parameter is empty!");
            return resMap;
        }

        String orgInput = null;
        String idInput = null;
        String check = null;
        String calError = null;
        if (selelcttype.equals("selectbyorg") && !input.equals("_")) {
            orgInput = input;
        } else if (selelcttype.equals("selectbyid") && !input.equals("_")) {
            idInput = input;
        }
        // 处理参数
        if (!checkstatus.equals("03")) {
            check = checkstatus;
        }
        if (!calerror.equals("_")) {
            calError = calerror;
        }


        //查询 台区电能表失准计算结果表 和 d_meter 表
        Map<String, Object> data = tErrorComputingService.selectErrorAndMeter(calError, check, orgInput, idInput, year, scope, orgNo7, page, size);
        resMap.put(STATUS, OK);
        resMap.put(DATA,data);
        return resMap;
    }


    /**
     * 3.1电能表按供电单位统计接口
     * http://localhost:9001/frontend/mp/org/1/5
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/org/{page}/{size}")
    public Map<String, Object> statisticsByOrgPowerSupplyUnit(HttpServletRequest request,
                                                           @PathVariable Integer page, @PathVariable Integer size) {

        Map<String,Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);

        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (null == page || null == size) {
            resMap.put(DES,"page and size parameter is empty!");
            return resMap;
        }

        Map<String, Object> data = tErrorComputingService.statisticsByPowerSupplyUnit(year, scope, orgNo7, page, size);
        resMap.put(STATUS, OK);
        resMap.put(DATA, data);
        return resMap;
    }






    /**
     * 3.2异常电能表数量变化趋势接口
     * http://localhost:9001/frontend/mp/errNumChange/1234567
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/mp/errNumChange")
    public Map<String,Object> errorNumChangeByMeterId(HttpServletRequest request) throws Exception{


        Map<String,Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);

        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        //获取时间
        String year = request.getHeader("dateStr");
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }

        Map<String, Object> data = tErrorComputingService.errorNumChangeByTgIdOrMeterId(orgNo7, year);

        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }


    /**
     * 3.3电能表按异常类型统计接口
     * 调试url 可参考 http://localhost:9001/frontend/mp/alarmtype/1/5/123456722
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/alarmtype/{page}/{size}/{orgNo}")
    public Map<String,Object> statisticsByExceptionUnit(HttpServletRequest request,@PathVariable Integer page, @PathVariable Integer size,
                                                        @PathVariable String orgNo){
        Map<String,Object> resMap = new HashMap<>();
        //获取时间
        String year = request.getHeader("dateStr");
        resMap.put(STATUS, ERROR);

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (StringUtils.isBlank(orgNo)) {
            resMap.put(DES, "orgNo parameter is empty!");
            return resMap;
        }
        if (null == page || null == size) {
            resMap.put(DES,"page and size parameter is empty!");
            return resMap;
        }
        String orgNo7 = null;
        if (!orgNo.equals("_")) {
            orgNo7 = orgNo;
        }

        int distinctType = 1; //count(distinct(meter_id))

        //按异常类型查询数据
        Map<String, Object> data = tErrorComputingService.selectByAlarmCode(year, page, size, orgNo7, distinctType);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     * 3.14电能表按异常类型统计接口 (异常电能表类型分布 大饼)
     * 调试url 可参考 http://localhost:9001/frontend/mp/alarmtype/_
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/alarmtype/{org7Id}")
    public Map<String, Object> statisticsByExceptionCode(HttpServletRequest request, @PathVariable("org7Id") String org7Id) {
        Map<String, Object> resMap = new HashMap<>();
        //获取时间
        resMap.put(STATUS, ERROR);
        String year = request.getHeader("dateStr");

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (!"_".equals(org7Id)) {
            orgNo7 = org7Id;
        }
        int distinctType = 1; //count(distinct(meter_id))

        //按异常类型查询数据
        List<StatisticsByExceptionVO> data = tErrorComputingService.selectByAlarmCodeNoPage(year, orgNo7, distinctType);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     * 3.4电能表按供电公司维度统计异常变化趋势接口
     * http://localhost:9001/frontend/mp/alarmorg
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/mp/alarmorg")
    public Map<String,Object> alarmOrg(HttpServletRequest request) throws Exception{


        Map<String,Object> resMap = new HashMap<>();
        //获取时间
        String year = request.getHeader("dateStr");
        resMap.put(STATUS, ERROR);

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        //标识 true 为 distinct tgid  false为 distinct meterid
        Boolean isTgid = true;
        Map<String, Object> data = tErrorComputingService.alarmOrg(year, scope, orgNo7, isTgid);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }



    /**
     * 3.6全部电能表按节点名/电能表编号/资产编号模糊查询接口
     * 调试url 可参考 http://localhost:9001/frontend/mp/all/selectbyorg/_/测试/1/5
     * @param selecttype
     * @param status
     * @param keyword
     * @param page
     * @param size
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/mp/all/{selecttype}/{status}/{keyword}/{page}/{size}", method=RequestMethod.GET)
    public Map<String,Object> selectAllMeterByNameOrId(@PathVariable String selecttype,
                                       @PathVariable String status, @PathVariable String keyword,
                                       @PathVariable Integer page,@PathVariable Integer size,
                                       HttpServletRequest request){

        Map<String,Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);

        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (null == page || null == size) {
            resMap.put(DES,"page and size parameter is empty!");
            return resMap;
        }

        String orgInput = null;
        String idInput = null;
        if (selecttype.equals("selectbyorg") && !keyword.equals("_")) {
            orgInput = keyword;
        } else if (selecttype.equals("selectbyid") && !keyword.equals("_")) {
            idInput = keyword;
        }


        //查询 台区电能表失准计算结果表 和 d_meter 表

        Map<String, Object> data = tErrorComputingService.selectAllMeterLike(orgInput, idInput, year, scope, orgNo7, page, size,0);
        resMap.put(STATUS, OK);
        resMap.put(DATA,data);
        return resMap;
    }




    /**
     * 3.7 正常表/观察电能表明细, 按节点名/电能表编号/资产编号模糊查询
     * 调试url 可参考 http://localhost:9001/frontend/mp/normal/selectbyorg/normal/_/1/5
     * @param selecttype
     * @param metertype
     * @param keyword
     * @param page
     * @param size
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/mp/normal/{selecttype}/{metertype}/{keyword}/{page}/{size}", method=RequestMethod.GET)
    public Map<String,Object> selectNormalMeterByNameOrId(@PathVariable String selecttype,
                                                       @PathVariable String metertype, @PathVariable String keyword,
                                                       @PathVariable Integer page,@PathVariable Integer size,
                                                       HttpServletRequest request){

        Map<String,Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);

        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (null == page || null == size || null == metertype) {
            resMap.put(DES,"page/size/metertype parameter is empty!");
            return resMap;
        }

        String orgInput = null;
        String idInput = null;
        if (selecttype.equals("selectbyorg") && !keyword.equals("_")) {
            orgInput = keyword;
        } else if (selecttype.equals("selectbyid") && !keyword.equals("_")) {
            idInput = keyword;
        }
        int status = 0;
        if (metertype.equals("normal")) {
            status = 1;
        } else if (metertype.equals("test")) {
            status = 3;
        }


        //查询 台区电能表失准计算结果表 和 d_meter 表

        Map<String, Object> data = tErrorComputingService.selectAllMeterLike(orgInput, idInput, year, scope, orgNo7, page, size, status);
        resMap.put(STATUS, OK);
        resMap.put(DATA,data);
        return resMap;
    }






    /**
     * 3.8异常电能表明细接口,按节点名/电能表编号/资产编号模糊查询
     * 调试url 可参考 http://localhost:9001/frontend/mp/alarm/selectbyorg/测试/1/5
     * @param selecttype
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/alarm/{selecttype}/{keyword}/{page}/{size}")
    public Map<String,Object> statisticsByAlarmPowerSupplyUnit(@PathVariable String selecttype,@PathVariable String keyword,
                                                                      @PathVariable Integer page, @PathVariable Integer size,
                                                          HttpServletRequest request){
        Map<String,Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);
        String orgInput = null;
        String idInput = null;
        if (selecttype.equals("selectbyorg") && !keyword.equals("_")) {
            orgInput = keyword;
        } else if (selecttype.equals("selectbyid") && !keyword.equals("_")) {
            idInput = keyword;
        }
        //获取header参数
        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }

        //查询异常表数据库
        Map<String, Object> data = tErrorComputingService.selectByNameOrIdLike(year, scope, orgNo7, orgInput, idInput, page, size);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     * 3.9电能表详情接口
     * 调试url 可参考 http://localhost:9001/frontend/mp/baseinfo/1
     * @param meterid
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/baseinfo/{meterid}")
    public Map<String,Object> getMeterInfo(@PathVariable String meterid){

        Map<String,Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        if (StringUtils.isBlank(meterid)) {
            resMap.put(DES, "meterid parameter error!");
            return resMap;
        }

        Map<String, Object> data = tErrorComputingService.getMeterInfo(meterid);
        resMap.put(STATUS, OK);
        resMap.put("data", data);

        return resMap;
    }


    /**
     * 3.10 电能表用电量接口
     * http://localhost:9001/frontend/mp/power/1/1/1/5
     *
     * @param meterid
     * @param isallmeter
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/power/{isallmeter}/{meterid}")
    public Map<String, Object> electricMeterPowerByMeterid(@PathVariable String meterid, @PathVariable Integer isallmeter, HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);
        if (StringUtils.isBlank(meterid) || isallmeter == null) {
            resMap.put(DES, "meterid/isallmeter parameter is null!");
            return resMap;
        }
        //获取时间
        String year = request.getHeader("dateStr");
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        try {
            List<MeterPowerVO> data = tErrorComputingService.electricMeterPowerByMeterid(meterid, isallmeter, null, year);
            resMap.put(STATUS, OK);
            resMap.put("data", data);
        } catch (Exception e) {
            log.error(e.getMessage());
            resMap.put(DES, "Failed to get data!");
            return resMap;
        }

        return resMap;
    }


    /**
     * 3.11电能表运行误差趋势接口
     * 调试url 可参考 http://localhost:9001/frontend/mp/alarmtrend/8008000048621813
     *
     * @param meterid
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/alarmtrend/{meterid}")
    public Map<String, Object> meterRunningError(@PathVariable String meterid, HttpServletRequest request) {

        Map<String, Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);
        if (StringUtils.isBlank(meterid)) {
            resMap.put(DES, "meterid parameter is null!");
            return resMap;
        }
        //获取时间
        String year = request.getHeader("dateStr");
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        try {
            Map<String, Object> data = tErrorComputingService.meterRunningError(meterid, year);
            resMap.put("data", data);
        } catch (Exception e) {
            log.error(e.getMessage());
            resMap.put(DES, "Failed to get data!");
            return resMap;
        }
        resMap.put(STATUS, OK);
        return resMap;
    }

    /**
     * 3.13异常电能表信息接口
     * http://localhost:9001/frontend/mp/alarmlist/8008000045503340/1/5
     * @param meterid
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/alarmlist/{meterid}/{page}/{size}")
    public Map<String, Object> gerErrorInfoByMeterid(@PathVariable String meterid,@PathVariable Integer page,
                                                     @PathVariable Integer size) {
        Map<String,Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        if (StringUtils.isBlank(meterid)) {
            resMap.put(DES, "meterid parameter is empty!");
            return resMap;
        }
        if (page == null || size == null) {
            resMap.put(DES, "page or size parameter is empty!");
            return resMap;
        }

        Map<String, Object> data = tErrorComputingService.gerErrorInfoByMeteridOrTgId(meterid, page, size, null, null, null);
        resMap.put(STATUS, OK);
        resMap.put("data",data);
        return resMap;

    }

    /**
     * 3.15 电能表按台区按供电单位统计接口 (大饼)
     * http://localhost:9001/frontend/tg/org
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/org")
    public Map<String, Object> powerSupplyGroupByTgMeterNumNoPage(HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        //获取header参数
        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        List<MeterVO> data = tErrorComputingService.powerSupplyGroupByTgMeterNumNoPage(year, scope, orgNo7);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }

    /**
     * 3.16 异常电能表数量分布图
     * http://localhost:9001/frontend/mp/meterNum/0222/_
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/mp/meterNum/{alarmCode}/{orgNo7}")
    public Map<String, Object> getAlramMeterNumByAlramCodeAndON7(@PathVariable String alarmCode, @PathVariable String orgNo7, HttpServletRequest request) {

        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        //获取header参数
        if (StringUtils.isBlank(alarmCode) || StringUtils.isBlank(orgNo7)) {
            resMap.put(DES, "alramCode/orgNo7 parameter is empty!");
            return resMap;
        }
        String on7Param = null;
        if (!orgNo7.equals("_")) {
            on7Param = orgNo7;
        } else {
            //获取供电单位ID
            if (!"_".equals(request.getHeader("org"))) {
                on7Param = request.getHeader("org");
            }
        }
        Map<String, Object> data = tErrorComputingService.getAlramMeterNumByAlramCodeAndON7(alarmCode, on7Param);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }
    /**
     * 3.16 异常电能表数量分布图
     * http://localhost:9001/frontend/mp/meterNum/0222/_
     *
     * @param request
     * @return
     */
    /*@ResponseBody
    @GetMapping("/mp/meterErrcODE/{alarmCode}/{orgNo7}")
    public Map<String, Object> getAlramMeterNumByAlramCodeAndON7(@PathVariable String alarmCode, @PathVariable String orgNo7, HttpServletRequest request) {

        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        //获取header参数
        if (StringUtils.isBlank(alarmCode) || StringUtils.isBlank(orgNo7)) {
            resMap.put(DES, "alramCode/orgNo7 parameter is empty!");
            return resMap;
        }
        String on7Param = null;
        if (!orgNo7.equals("_")) {
            on7Param = orgNo7;
        }
        Map<String, Object> data = tErrorComputingService.getAlramMeterNumByAlramCodeAndON7(alarmCode, on7Param);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }*/

    /**
     * 4.1按台区按供电单位统计接口 (已调)
     * http://localhost:9001/frontend/tg/org/1/5
     * @param request
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/org/{page}/{size}")
    public Map<String, Object> powerSupplyGroupByTg(HttpServletRequest request,@PathVariable Integer page,
                                                    @PathVariable Integer size) {
        Map<String,Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        //获取header参数
        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (page == null || size == null) {
            resMap.put(DES, "page or size parameter is empty!");
            return resMap;
        }

        Map<String, Object> data = tErrorComputingService.powerSupplyGroupByOrgNo7(year, scope, orgNo7, page, size);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     * 4.2异常台区数量变化趋势接口 默认
     * http://localhost:9001/frontend/tg/errNumChange/3004547260/2021-6-1/2021-6-30
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/tg/errNumChange/{orgNo}")
    public Map<String, Object> errorNumChangeByTgId(@PathVariable String orgNo, HttpServletRequest request) throws Exception {


        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        if (StringUtils.isBlank(orgNo)) {
            resMap.put(DES, "orgNo parameter is empty!");
        }
        //获取时间
        String year = request.getHeader("dateStr");
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        Map<String, Object> data = tErrorComputingService.errorNumChangeByTgIdOrMeterId(orgNo,year);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }

    /**
     * 4.3台区按异常类型统计接口  同 3.3sercvice  参数区分
     * http://localhost:9001/frontend/mp/alarmtype/1/5/123456722
     * @param request
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/alarmtype/{page}/{size}/{orgNo}")
    public Map<String, Object> alarmtypeGroupByTg(HttpServletRequest request, @PathVariable Integer page, @PathVariable Integer size,
                                                  @PathVariable String orgNo) {
        Map<String,Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        //获取header参数
        //获取时间
        String year = request.getHeader("dateStr");

        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (page == null || size == null) {
            resMap.put(DES, "page or size parameter is empty!");
            return resMap;
        }
        int distinctType = 2; //count（distinct（tg_id））

        String orgNo7 = null ;
        if (!orgNo.equals("_")) {
            orgNo7 = orgNo;
        } else {
            if (!"_".equals(request.getHeader("org"))) {
                orgNo7 = request.getHeader("org");
            }
        }

        Map<String, Object> data = tErrorComputingService.selectByAlarmCode(year, page, size, orgNo7, distinctType);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }


    /**
     * 4.4 台区按供电公司维度统计异常变化趋势接口
     * http://localhost:9001/frontend/tg/alarmorg
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/tg/alarmorg")
    public Map<String,Object> tgAlarmOrg(HttpServletRequest request) {

        Map<String,Object> resMap = new HashMap<>();
        //获取时间
        String year = request.getHeader("dateStr");
        resMap.put(STATUS, ERROR);

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        //标识 true 为 distinct tgid  false为 distinct meterid
        Boolean isTgid = true;

        Map<String, Object> data = tErrorComputingService.alarmOrg(year, scope, orgNo7, isTgid);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }

    /**
     * 4.6 全部台区按节点名模糊查询接口
     * http://localhost:9001/frontend/tg/all/_/所/1/5
     *
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("tg/all/{status}/{keyword}/{page}/{size}")
    public Map<String, Object> selectAllTgByOrgName(@PathVariable String keyword, @PathVariable String status,
                                                    @PathVariable int page, @PathVariable int size, HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        String tgStatus = null;
        if (!status.equals("_")) {
            tgStatus = status;
        }

        Map<String, Object> data = tErrorComputingService.selectAllTgByOrgName(keyword, tgStatus, page, size,orgNo7);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }


    /**
     * 4.7异常台区按节点名模糊查询接口
     * http://localhost:9001/frontend/tg/alarm/所/_/1/5
     * @param request
     * @param page
     * @param size
     * @param keyword
     * @return
     */
    @ResponseBody
    @GetMapping("tg/alarm/{keyword}/{tgStatus}/{page}/{size}")
    public Map<String, Object> getErrorByOrgNameLike(HttpServletRequest request, @PathVariable Integer page, @PathVariable Integer size,
                                           @PathVariable String keyword,@PathVariable String tgStatus) {

        Map<String,Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        //获取header参数
        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        if (page == null || size == null) {
            resMap.put(DES, "page or size parameter is empty!");
            return resMap;
        }

        Map<String, Object> data = tErrorComputingService.getErrorByOrgNameLike(year, scope, orgNo7, page, size, keyword);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     * 4.8新建台区按节点名模糊查询接口
     * http://localhost:9001/frontend/tg/new/所/1/5
     *
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/new/{keyword}/{page}/{size}")
    public Map<String, Object> selectNewTgByOrgName(@PathVariable String keyword, @PathVariable int page,
                                                    @PathVariable int size, HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        Map<String, Object> data = tErrorComputingService.selectNewTgByOrgName(keyword, page, size,orgNo7);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }

    /**
     * 4.9正常台区按节点名模糊查询接口
     * http://localhost:9001/frontend/tg/normal/所/1/5
     *
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/normal/{keyword}/{page}/{size}")
    public Map<String, Object> selectNormalTgByOrgName(@PathVariable String keyword, @PathVariable int page,
                                                       @PathVariable int size, HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }

        Map<String, Object> data = tErrorComputingService.selectNormalTgByOrgName(keyword, page, size, orgNo7);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }


    /**
     * 4.10台区详情接口
     * http://localhost:9001/frontend/tg/baseinfo/3004547260
     *
     * @param tgid
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/baseinfo/{tgid}")
    public Map<String, Object> getBaseInfoByTgId(@PathVariable String tgid, HttpServletRequest request) {
        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        String year = request.getHeader("dateStr");
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }

        Map<String, Object> data = tErrorComputingService.getBaseInfoByTgId(tgid, year);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;

    }


    /**
     * 4.11 台区用电量接口
     * http://localhost:9001/frontend/tg/power/3004547260/1/5
     * @param tgid
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/power/{tgid}")
    public Map<String, Object> electricMeterPowerByTgid(@PathVariable String tgid) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);
        if (StringUtils.isBlank(tgid)) {
            resMap.put(DES, "tgid is null!");
            return resMap;
        }

        List<MeterPowerVO> data = tErrorComputingService.electricMeterPowerByTgid(tgid);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }
    /**
     * 4.12 台区电能表运行误差趋势
     * http://localhost:9001/frontend/tg/power/3004547260/1/5
     * @param tgid
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/alarmtrend/{tgid}")
    public Map<String, Object> meterRunningErrorByTgId(@PathVariable String tgid) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put(STATUS, ERROR);
        if (StringUtils.isBlank(tgid)) {
            resMap.put(DES, "tgid is null!");
            return resMap;
        }
        Map<String,Object> data = tErrorComputingService.meterRunningErrorByTgId(tgid);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }




    /**
     * 4.13异常电能表信息接口
     * http://localhost:9001/frontend/tg/alarmlist/3004547260/_/_1/5
     *
     * @param tgid
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/alarmlist/{tgid}/{alarmType}/{alarmTime}/{page}/{size}")
    public Map<String, Object> getNewTgInfoByOrgNameLike(@PathVariable String tgid, @PathVariable Integer page, @PathVariable Integer size,
                                                         @PathVariable String alarmType, @PathVariable String alarmTime) {

        Map<String, Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);

        if (StringUtils.isBlank(tgid)) {
            resMap.put(DES, "meterid parameter is empty!");
            return resMap;
        }
        if (page == null || size == null) {
            resMap.put(DES, "page or size parameter is empty!");
            return resMap;
        }
        String alarmTypeParam = null;
        String alarmTimeParam = null;
        if (!alarmTime.equals("_")) {
            alarmTimeParam = alarmTime;
        }
        if (!alarmType.equals("_")) {
            alarmTypeParam = alarmType;
        }

        Map<String, Object> data = tErrorComputingService.gerErrorInfoByMeteridOrTgId(null, page, size, tgid,alarmTypeParam,alarmTimeParam);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


    /**
     *
     * 4.14异常电能表信息接口
     * http://localhost:9001/frontend/tg/alarmtype
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/alarmtype")
    public Map<String, Object> getNewTgInfoByOrgNameLikeNoPage(HttpServletRequest request) {

        Map<String,Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);

        //获取header参数
        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        int distinctType = 2; //distinct tgId
        List<StatisticsByExceptionVO> data = tErrorComputingService.selectByAlarmCodeNoPage(year, orgNo7, distinctType);
        resMap.put(STATUS, OK);
        resMap.put("data",data);
        return resMap;
    }

    /**
     * 4.15 按台区按供电单位统计接口 (大饼)
     * http://localhost:9001/frontend/tg/org
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/tg/org")
    public Map<String, Object> powerSupplyGroupByTgNoPage(HttpServletRequest request) {
        Map<String,Object> resMap = new HashMap<>();

        resMap.put(STATUS, ERROR);
        //获取header参数
        //获取时间
        String year = request.getHeader("dateStr");

        //获取城市ID
        String scope = null;
        if (!"_".equals(request.getHeader("scope"))) {
            scope = request.getHeader("scope");
        }

        //获取供电单位ID
        String orgNo7 = null;
        if (!"_".equals(request.getHeader("org"))) {
            orgNo7 = request.getHeader("org");
        }
        if (StringUtils.isBlank(year)) {
            resMap.put(DES, "year parameter is empty!");
            return resMap;
        }
        List<PowerSupplyGroupByTgVO> data = tErrorComputingService.powerSupplyGroupByTgNoPage(year, scope, orgNo7);
        resMap.put(STATUS, OK);
        resMap.put("data", data);
        return resMap;
    }


}
